package com.cs.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cs.domain.entity.Goods;
import com.cs.domain.entity.StoreGoods;
import com.cs.goods.dao.IGoodsDao;
import com.cs.store.dao.IJoinStoreGoodsDao;
import com.cs.store.dao.IStoreGoodsDao;
import com.cs.store.service.IStoreGoodsService;
import com.cs.store.vo.JoinStoreGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StoreGoodsServiceImpl implements IStoreGoodsService {
    @Autowired
    private IJoinStoreGoodsDao iJoinStoreGoodsDao;
    @Autowired
    private IStoreGoodsDao iStoreGoodsDao;

    @Autowired
    private IGoodsDao iGoodsDao;


    /**
     * 商家端查询所有入库商品
     *
     * @param storeId
     * @return
     */
    @Override
    public List<JoinStoreGoods> findAllGoodsByStoreIdOnline(Long storeId) {
        QueryWrapper<JoinStoreGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId)
                .eq("store_goods_stack_status", 1L);
        List<JoinStoreGoods> list = iJoinStoreGoodsDao.selectList(queryWrapper);

        return list;
    }
    @Override
    public List<JoinStoreGoods> findAllGoodsByStoreId(Long storeId) {
        QueryWrapper<JoinStoreGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        List<JoinStoreGoods> list = iJoinStoreGoodsDao.selectList(queryWrapper);
        return list;
    }




    /**
     * 查询商家已经上架的商品分类
     *
     * @param storeId
     * @return
     */
    @Override
    public List<JoinStoreGoods> findTypeByStoreIdOnline(Long storeId) {
        QueryWrapper<JoinStoreGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type_id", "type_name")
                .eq("store_goods_stack_status", 1L)
                .eq("store_id", storeId)
                .groupBy("type_id", "type_name");
        List<JoinStoreGoods> list = iJoinStoreGoodsDao.selectList(queryWrapper);
        return list;
    }
    @Override
    public List<JoinStoreGoods> findTypeByStoreId(Long storeId) {
        QueryWrapper<JoinStoreGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type_id", "type_name")
                .eq("store_id", storeId)
                .groupBy("type_id", "type_name");
        List<JoinStoreGoods> list = iJoinStoreGoodsDao.selectList(queryWrapper);
        return list;
    }






    /**
     * 商家已经上架的商品，根据分类查看
     *
     * @param typeId
     * @param storeId
     * @return
     */
    @Override
    public List<JoinStoreGoods> findGoodsByTypeIdOnline(Long typeId, Long storeId) {
        QueryWrapper<JoinStoreGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId)
                .eq("store_goods_stack_status", 1L)
                .eq("type_id", typeId);
        List<JoinStoreGoods> list = iJoinStoreGoodsDao.selectList(queryWrapper);

        return list;
    }
    @Override
    public List<JoinStoreGoods> findGoodsByTypeId(Long typeId, Long storeId) {
        QueryWrapper<JoinStoreGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId)
                .eq("type_id", typeId);
        List<JoinStoreGoods> list = iJoinStoreGoodsDao.selectList(queryWrapper);

        return list;
    }



    /**
     * 从goods表中添加商品到自己店铺中
     * 需要插入的字段有：商品id、商家id、过期时间、库存量，
     * 默认上架状态、是否临期、是否爆款为0
     *
     * @param storeGoods
     */
    @Override
    public void addStoreGoods(StoreGoods storeGoods) {
        iStoreGoodsDao.insert(storeGoods);
    }

    /**
     * 修改本店铺中的商品信息
     * 根据JoinStoreGoods表中的store_goods_id，获取到store_goods对象，修改后再更新到数据库
     * 商品id、商家id 不可以被修改
     *
     * @param storeGoods
     */
    @Override
    public void updStoreGoods(StoreGoods storeGoods) {
        iStoreGoodsDao.updateById(storeGoods);
    }

    /**
     * 根据根据JoinStoreGoods表中的store_goods_id，删除store_goods对象
     * @param storeGoodsId
     */
    @Override
    public void delStoreGoods(Long storeGoodsId) {
        iStoreGoodsDao.deleteById(storeGoodsId);
    }

    /**
     * 根据JoinStoreGoods表中的store_goods_id，获取到store_goods对象
     * @param storeGoodsId
     * @return
     */
    @Override
    public StoreGoods findTheStoreGoods(String storeGoodsId) {
        return iStoreGoodsDao.selectById(storeGoodsId);
    }


    /**
     * 查询管理端所有商品
     * @return
     */
    @Override
    public List<Goods> findGoods() {
        return iGoodsDao.selectList(null);
    }

}
